package com.zhang.modules.mianShi.controller;

import com.zhang.common.jpa.entity.impl.BasePageCmd;
import com.zhang.common.web.respWrapper.Res;
import com.zhang.modules.api.annotation.ApiPermission;
import com.zhang.modules.mianShi.entity.MachineGroup;
import com.zhang.modules.mianShi.entity.cmd.machine.MachineAddCmd;
import com.zhang.modules.mianShi.entity.cmd.machine.MachineEditCmd;
import com.zhang.modules.mianShi.service.IMachineGroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
@Api(tags = "机组管理")
@RequestMapping("/api/machine")
@ApiPermission("app:machine:*")
public class MachineGroupController {


    private IMachineGroupService service;

    public MachineGroupController(IMachineGroupService service) {
        this.service = service;
    }

    @ApiOperation("机组添加")
    @ApiPermission("app:machine:add")
    @PostMapping("/add")
    public Res add(@RequestBody @Valid MachineAddCmd addCmd){
        service.save(addCmd, MachineGroup::new);
        return Res.ok("添加成功");
    }


    @ApiOperation("机组删除")
    @ApiPermission("app:machine:delete")
    @GetMapping("/delete")
    public Res delete(@Valid @NotBlank(message = "id不能为空")String id){
        service.deleteLogic(id);
        return Res.ok("删除成功");
    }


    @ApiOperation("机组编辑")
    @ApiPermission("app:machine:edit")
    @PostMapping("/edit")
    public Res edit(@RequestBody @Valid MachineEditCmd editCmd){
        service.update(editCmd);
        return Res.ok("更新成功");
    }


    @ApiOperation("机组列表")
    @ApiPermission("app:machine:page")
    @GetMapping("/page")
    public Res page(@Valid BasePageCmd pageCmd){
        Page<MachineGroup> result = service.page(pageCmd);
        return Res.ok(result);
    }


}
